package com.elong.qadev.task;

import java.util.ArrayList;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.elong.qadev.entity.TaskInfo;
import com.elong.qadev.service.TaskServiceI;
/**
 * 
* @ClassName: PreloadListener
* @Description: TODO(初始化自动执行TASK)
* @author kangchen
* @date 2015年9月25日
*
 */
public class PreloadListener implements ServletContextListener {
	Logger logger = Logger.getLogger(PreloadListener.class);
	public TaskServiceI taskService;
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		logger.debug("执行启动初始化QuartzManager");
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				new String[] { "spring.xml", "spring-hibernate.xml" });
		taskService = (TaskServiceI) ac.getBean("taskService");
		ArrayList<TaskInfo> ati = taskService.getTaskInfo();
		TaskInfo o = new TaskInfo();
		QuartzManager.addJob("server心跳检查","com.elong.qadev.task.ServerHeartCheck","0/30 * * ? * *",o);
		
		for (int i = 0; i < ati.size(); i++) {
			TaskInfo ti = new TaskInfo();
			ti=ati.get(i);
			logger.debug("提取的taskInfo数据为:"+ti.toString());
			QuartzManager.addJob(ti.getTaskName(), "com.elong.qadev.task.RunCaseJob", ti.getTimeConfig(),ti);
		}
		
		QuartzManager.startJobs();
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		logger.debug("执行关闭摧毁QuartzManager");
		QuartzManager.shutdownJobs();
	}

}
